package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.EquipmentsBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.EquipmentsMapper;
import com.java2207.group1.pojo.Equipments;
import com.java2207.group1.pojo.Expenditure;
import com.java2207.group1.service.EquipmentsService;
import com.java2207.group1.service.FinancialManagementService;
import com.java2207.group1.vo.equipment.BarVo;
import com.java2207.group1.vo.equipments.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lz
 * @Date 2022/7/26 16:48
 */
@Service("equipmentsService")
public class EquipmentsServiceImpl implements EquipmentsService {

    @Autowired
    private EquipmentsMapper equipmentsMapper;

    @Override
    public LayuiPageVo<Equipments> equipmentList(@RequestParam Map<String, Object> params) {

        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);

        List<Equipments> list = equipmentsMapper.equipmentList(params);

        PageInfo<Equipments> equipmentPageInfo = new PageInfo<>(list);

        LayuiPageVo<Equipments> equipmentLayuiPageVo = new LayuiPageVo<>();

        equipmentLayuiPageVo.setCode(0);
        equipmentLayuiPageVo.setMsg("分页列表数据");
        equipmentLayuiPageVo.setCount(equipmentPageInfo.getTotal());
        equipmentLayuiPageVo.setData(equipmentPageInfo.getList());

        return equipmentLayuiPageVo;
    }

    @Override
    public int updateEquipment(Equipments equipment) {
        return equipmentsMapper.updateEquipment(equipment);
    }

    @Autowired
    private FinancialManagementService financialManagementService;

    @Override
    public int addEquipment(Equipments equipments) {
        Expenditure expenditure = new Expenditure(equipments.getId(), equipments.getPayor(), equipments.getContactPayor(), equipments.getUserOfAssets(), equipments.getExpenseAmount(), equipments.getDisbursementDate());
        financialManagementService.addExpenditure(expenditure);
        return equipmentsMapper.addEquipment(equipments);
    }

    @Override
    public int deleteEquipment(int id) {
        return equipmentsMapper.deleteEquipment(id);
    }

    @Override
    public List<BarVo> selectEchart() {
        return equipmentsMapper.selectEchart();
    }

    @Override
    public List<PieVo> selectPie() {
        return equipmentsMapper.selectPie();
    }

    @Override
    public List<EquipmentsBo> equipmentsExcelData(Equipments equipments) {
        List<Equipments> orders = equipmentsMapper.selectExcel(equipments);

        List<EquipmentsBo> equipmentsBos = orders.stream()
                .map((stu) -> {
                    EquipmentsBo equipmentsBo = new EquipmentsBo();
                    equipmentsBo.setId(stu.getId());
                    equipmentsBo.setName(stu.getName());
                    equipmentsBo.setRepertory(stu.getRepertory());
                    return equipmentsBo;
                }).collect(Collectors.toList());

        return equipmentsBos;
    }

}
